/*
 *                  BioJava development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the individual
 * authors.  These should be listed in @author doc comments.
 *
 * For more information on the BioJava project and its aims,
 * or to join the biojava-l mailing list, visit the home page
 * at:
 *
 *      http://www.biojava.org/
 * 
 * Created on Feb 6, 2006
 *
 */
package org.biojava.services.das2;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.sql.DataSource;

import org.biojava.dasobert.dasregistry.DasCoordinateSystem;
import org.biojava.dasobert.dasregistry.DasSource;
import org.biojava.services.das.dao.OrganismBean;
import org.biojava.services.das.dao.ProjectBean;
import org.biojava.services.das.dao.UserDAO;
import org.biojava.services.das.registry.DasRegistrySql;
import org.biojava.services.das.registry.UserManager;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Das1SourceDbProvider implements DasSourceProvider {

	DasRegistrySql registry ;

	

	public DasRegistrySql getRegistry() {
		return registry;
	}

	public void setRegistry(DasRegistrySql registry) {
		this.registry = registry;
	}

	

	UserDAO dao = new UserDAO();
	UserManager userManager;


	public UserManager getUserManager() {
		return userManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public Das1SourceDbProvider() {
		super();
//		BeanFactory ctx=(new ClassPathXmlApplicationContext("SpringDAO.xml"));
//		dbregistry=(DasRegistrySql)ctx.getBean("registryBean");
//  		manager=(UserManager)ctx.getBean("userManager");
	}

	public List<DasSource> getDasSources() {

		List<DasSource> sources = registry.listServices();
		Connection conn = null;
		try {

			DataSource dataSource = registry.getDataSource();
			conn = dataSource.getConnection();
			
	  		
			for (DasSource source: sources){

//System.out.println(" source nickname"+source.getNickname()+" source url="+source.getUrl());
				ProjectBean p = dao.getProjectForURL(userManager, conn, source.getUrl());
				if ( p == null)
					continue;
				Map<String,String> props = source.getProperties();
				props.put("projectName", p.getName());
				props.put("projectIcon", "http://www.dasregistry.org/"+p.getIconURL());
				props.put("projectHome", p.getHomepage().toString());
				props.put("projectDesc", p.getDescription());
				source.setProperties(props);
			}
		} catch (SQLException e){
			e.printStackTrace();
		} finally {
			if ( conn != null){
				try{
					conn.close();
				} catch (SQLException e){
					e.printStackTrace();
				}
			}
		}

		return sources;

	}
	
	@Deprecated
	public DasSource getSingleDasSource(String id){


		DasSource source = registry.getDasSource(id);
		DataSource dataSource = registry.getDataSource();
		Connection conn = null;
		try {

			conn = dataSource.getConnection();
			ProjectBean p = dao.getProjectForURL(userManager ,conn, source.getUrl());
			if ( p != null) {

				Map<String,String> props = source.getProperties();
				props.put("projectName", p.getName());
				props.put("projectIcon", "http://www.dasregistry.org/"+p.getIconURL());
				props.put("projectHome", p.getHomepage().toString());
				props.put("projectDesc", p.getDescription());
				source.setProperties(props);
			}

		} catch (SQLException e){
			e.printStackTrace();
		} finally {
			if ( conn != null){
				try{
					conn.close();
				} catch (SQLException e){
					e.printStackTrace();
				}
			}
		}
		return source;        
	}

	public List<DasCoordinateSystem> getCoordinateSystems() {
System.out.println("using getCoordinateSystems in Das1SourceDbProvider");
		List<DasCoordinateSystem> dcs = new ArrayList();
		try {
			dcs = registry.getAllCoordinateSystems();
		} catch (Exception e){
			e.printStackTrace();
		}
		return dcs;

	}

	public OrganismBean[] getOrganisms() {

		return registry.getAllOrganisms();

	}
	
	/**
	 * get a map of source url to types info
	 * @return map of unique url to types info for each source in the registry
	 */
	public Map<String, List<Map<String,String>>> getAutoIdWithTypes(List<DasSource> sources){
		Map<String, List<Map<String,String>>> map=new HashMap<String, List<Map<String,String>>>();
		for(DasSource source:sources){
		int auto_id=registry.getAutoID4DasSource(source);
		List<Map<String, String>> types=registry.getTypes(String.valueOf(auto_id));
		map.put(source.getId(), types);
		}
		
		return map;
		
		
	}

	public Date getlastModified() {
	return registry.getlastModified();
	}
	



}
